﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace LookUpLater
{
    public partial class AddBox : Form
    {
        public AddBox()
        {
            InitializeComponent();
            LoadComponents();
        }
        IEnumerable<string> distnctTypes;
        IEnumerable<string> distnctSubCate;
        IEnumerable<string> distnctGroups;

        /// <summary>
        /// Loads the components.
        /// </summary>
        private void LoadComponents()
        {
            var category = Category.CurrentCategory;
            distnctTypes = category.Types.Select(t => t.Name).Distinct();
            distnctSubCate = category.Types.SelectMany(t => t.SubCategories).Select(s => s.Name).Distinct();
            distnctGroups = category.Types.SelectMany(t => t.SubCategories).SelectMany(s => s.Groups).Select(g => g.Name).Distinct();
            this.cmbGroup.Items.AddRange(distnctGroups.ToArray());
            this.cmbSubCat.Items.AddRange(distnctSubCate.ToArray());
            this.cmbType.Items.AddRange(distnctTypes.ToArray());
            if (Clipboard.ContainsText())
                this.txtLink.Text = Clipboard.GetText();

        }

        /// <summary>
        /// Adds the lookup.
        /// </summary>
        /// <returns></returns>
        private string AddLookup()
        {
            var groupText = string.IsNullOrWhiteSpace(this.txtGroup.Text) ? Convert.ToString(this.cmbGroup.SelectedItem) : this.txtGroup.Text;
            var subcattext = string.IsNullOrWhiteSpace(this.txtSubCat.Text) ? Convert.ToString(this.cmbSubCat.SelectedItem) : this.txtSubCat.Text;
            var typetext = string.IsNullOrWhiteSpace(this.txtType.Text) ? Convert.ToString(this.cmbType.SelectedItem) : this.txtType.Text;
            if (string.IsNullOrWhiteSpace(groupText) || string.IsNullOrWhiteSpace(subcattext) || string.IsNullOrWhiteSpace(typetext))
                return string.Empty;
            Group g = new Group(groupText);
            SubCategory s = new SubCategory(subcattext, new List<Group> { g });
            Type t = new Type(typetext, new List<SubCategory> { s });
            new Category(new List<Type> { t }).Save();

            var nametext = this.txtName.Text;
            var linktext = this.txtLink.Text;
            var descriptiontext = this.txtDescription.Text;
            if (string.IsNullOrWhiteSpace(nametext) || string.IsNullOrWhiteSpace(linktext) || string.IsNullOrWhiteSpace(descriptiontext))
                return string.Empty;
            var lookupId = Guid.NewGuid().ToString("N");
            Lookup lkp = new Lookup(nametext, linktext, descriptiontext, lookupId);
            ItemType it=new ItemType(t,s,g,new List<Lookup>{lkp});
            if (new LookUpLaterDB(new List<ItemType> { it }).Save())
                return lookupId;
            return string.Empty;
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            var lookupId = AddLookup();
            if (!string.IsNullOrWhiteSpace(lookupId))
            {
                LookUpStatus newSta = new LookUpStatus(Status.YetToLookUp, lookupId);
                newSta.SaveOrUpdate();
            }
            LookUpStatus.Refresh();
            LookUpLaterDB.Refresh();
        }


    }
}
